package com.acme.bankapp.service.bank;

import java.util.ArrayList;

import com.acme.bankapp.domain.bank.Account;
import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.bank.Client;
import com.acme.bankapp.domain.bank.Gender;

/**
 * Class which aggregates banks
 * and provide some operations with them
 * 
 * @author render1980
 *
 */
public class BankApplication {
	/**
	 * List of banks
	 */
	private ArrayList<Bank> banks = null;
	
	public BankApplication() {
		this.banks = new ArrayList<Bank>();
	}
	
	/**
	 * @param args	command line arguments
	 */
	public static void main(String[] args) {
		Bank bank1 = new Bank();
		Bank bank2 = new Bank();
		Client client1 = new Client("Josh", Gender.MALE);
		client1.setAccount(new CheckingAccount(35.00, 15.00));
		Client client2 = new Client("Valery", Gender.FEMALE);
		client2.setAccount(new SavingAccount(50.00));
		Client client3 = new Client("Greg", Gender.MALE);
		client3.setAccount(new SavingAccount(12.56));
		Client client4 = new Client("Craig", Gender.FEMALE);
		client4.setAccount(new CheckingAccount(3.33, 5.00));
		Client client5 = new Client("Dolores", Gender.FEMALE);
		client5.setAccount(new CheckingAccount(1.00, 3.00));
		
		bank1.addClient(client1);
		bank1.addClient(client2);
		bank1.addClient(client3);
		bank2.addClient(client4);
		bank2.addClient(client5);
		
		BankApplication bankApp = new BankApplication();
		bankApp.addBank(bank1);
		bankApp.addBank(bank2);
		bankApp.modifyBanks();
		bankApp.printBalance();
	}
	
	/**
	 * 
	 * @return	list of all banks in BankApplication
	 */
	public Bank[] getBanks() {
		return this.banks.toArray(new Bank[banks.size()]);
	}
	
	/**
	 *	
	 * @param bank	bank to add to the banks list
	 */
	public void addBank(Bank bank) {
		this.banks.add(bank);
	}
	
	/**
	 * Method to modify banks of BankApplication
	 * Can call two Account methods on the base of some condition:
	 * 	- Account.deposit
	 * 	- Account.withdraw
	 */
	public void modifyBanks() {
		for(Bank bank: this.banks) {
			Client[] bankClients = bank.getClients();
			for(int i = 0; i < bankClients.length; i++){
				Account[] clientAccounts = bankClients[i].getAccounts();
				for(Account acc: clientAccounts) {
					if(i % 2 == 0){
						acc.deposit(25.00);
					}
					else{
						acc.withdraw(5.00);
					}
				}
			}
		}
	}
	
	/**
	 * Method to print balance of each bank client account
	 */
	public void printBalance() {
		for(Bank bank: this.banks) {
			Client[] bankClients = bank.getClients();
			for(Client cl: bankClients){
				for(Account acc: cl.getAccounts()) {
					System.out.println(acc.getBalance());
				}
			}
		}
	}
}